home *** CD-ROM | disk | FTP | other *** search
- // Particle.cpp: implementation of the Particle class.
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "std.h"
- #include "Particle.h"
- #define MAXANGLES 16384
- extern int theTexture;
-
- // used to compute the min and max of two expresions
- #define MIN(a, b) (((a) < (b)) ? (a) : (b))
- #define MAX(a, b) (((a) > (b)) ? (a) : (b))
-
- inline float FastDistance2D(float x, float y)
- {
- // this function computes the distance from 0,0 to x,y with ~3.5% error
-
- // first compute the absolute value of x,y
- x = (x < 0.0f) ? -x : x;
- y = (y < 0.0f) ? -y : y;
-
- // compute the minimum of x,y
- float mn = MIN(x,y);
-
- // return the distance
- return(x+y-(mn*0.5f)-(mn*0.25f)+(mn*0.0625f));
-
- } // end FastDistance2D
-
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- Particle::Particle()
- {
- Init();
- }
-
- Particle::~Particle()
- {
-
- }
-
- void Particle::Draw() // the math was easier in 2D - so 2D it is
- {
- static int polylist = 0;
- // near clip
- if (z < 100.0f)
- {
- Init();
- return;
- }
- float screenx = (x * sys_glWidth / z) + sys_glWidth * 0.5f;
- // side clip
- if (screenx > sys_glWidth + 100.0f || screenx < -100.0f)
- {
- Init();
- return;
- }
- float screeny = (y * sys_glWidth / z) + sys_glHeight * 0.5f;
- // vertical clip
- if (screeny > sys_glHeight + 100.0f || screeny < -100.0f)
- {
- Init();
- return;
- }
-
- float oldscreenx = (oldx * sys_glWidth / oldz) + sys_glWidth * 0.5f;
- float oldscreeny = (oldy * sys_glWidth / oldz) + sys_glHeight * 0.5f;
-
- // glColor4f(r,g,b,1.0f);
- starfieldColor[starfieldColorIndex++] = r;
- starfieldColor[starfieldColorIndex++] = g;
- starfieldColor[starfieldColorIndex++] = b;
- starfieldColor[starfieldColorIndex++] = 1.0f;
- starfieldColor[starfieldColorIndex++] = r;
- starfieldColor[starfieldColorIndex++] = g;
- starfieldColor[starfieldColorIndex++] = b;
- starfieldColor[starfieldColorIndex++] = 1.0f;
- starfieldColor[starfieldColorIndex++] = r;
- starfieldColor[starfieldColorIndex++] = g;
- starfieldColor[starfieldColorIndex++] = b;
- starfieldColor[starfieldColorIndex++] = 1.0f;
- starfieldColor[starfieldColorIndex++] = r;
- starfieldColor[starfieldColorIndex++] = g;
- starfieldColor[starfieldColorIndex++] = b;
- starfieldColor[starfieldColorIndex++] = 1.0f;
-
- float dx = (screenx-oldscreenx);
- float dy = (screeny-oldscreeny);
-
- float m = FastDistance2D(dx, dy);// * 0.75f;
-
- animFrame++;
- if (animFrame == 64)
- {
- animFrame = 0;
- }
- float u0 = (animFrame&&7) * 0.125f;
- float v0 = (animFrame>>3) * 0.125f;
- float u1 = u0 + 0.125f;
- float v1 = v0 + 0.125f;
-
- float size = (3500.0f*(sys_glWidth/1024.0f));
-
- float w = max(1.5f,size/z);
- float ow = max(1.5f,size/oldz);
-
- float d = FastDistance2D(dx, dy);
-
- float s;
- if (d)
- {
- s = w/d;
- }
- else
- {
- s = 0.0f;
- }
- float os;
- if (d)
- {
- os = ow/d;
- }
- else
- {
- os = 0.0f;
- }
-
- m = 2.0f + s;
-
- float dxs = dx*s;
- float dys = dy*s;
- float dxos = dx*os;
- float dyos = dy*os;
- float dxm = dx*m;
- float dym = dy*m;
-
- // glBegin(GL_TRIANGLE_STRIP);
- // glTexCoord2f(u0,v0);
- starfieldTextures[starfieldTexturesIndex++] = u0;
- starfieldTextures[starfieldTexturesIndex++] = v0;
- // glVertex2f(screenx+dxm-dys,screeny+dym+dxs);
- starfieldVertices[starfieldVerticesIndex++] = screenx+dxm-dys;
- starfieldVertices[starfieldVerticesIndex++] = screeny+dym+dxs;
- // glTexCoord2f(u0,v1);
- starfieldTextures[starfieldTexturesIndex++] = u0;
- starfieldTextures[starfieldTexturesIndex++] = v1;
- // glVertex2f(screenx+dxm+dys,screeny+dym-dxs);
- starfieldVertices[starfieldVerticesIndex++] = screenx+dxm+dys;
- starfieldVertices[starfieldVerticesIndex++] = screeny+dym-dxs;
- // glTexCoord2f(u1,v1);
- starfieldTextures[starfieldTexturesIndex++] = u1;
- starfieldTextures[starfieldTexturesIndex++] = v1;
- // glVertex2f(oldscreenx-dxm+dyos,oldscreeny-dym-dxos);
- starfieldVertices[starfieldVerticesIndex++] = oldscreenx-dxm+dyos;
- starfieldVertices[starfieldVerticesIndex++] = oldscreeny-dym-dxos;
- // glTexCoord2f(u1,v0);
- starfieldTextures[starfieldTexturesIndex++] = u1;
- starfieldTextures[starfieldTexturesIndex++] = v0;
- // glVertex2f(oldscreenx-dxm-dyos,oldscreeny-dym+dxos);
- starfieldVertices[starfieldVerticesIndex++] = oldscreenx-dxm-dyos;
- starfieldVertices[starfieldVerticesIndex++] = oldscreeny-dym+dxos;
- // glEnd();
- }
-
- void Particle::Update()
- {
- oldx = x;
- oldy = y;
- oldz = z;
-
- x += deltax*fDeltaTime;
- y += deltay*fDeltaTime;
- z += deltaz*fDeltaTime;
- }
-
- void Particle::Init()
- {
- // float tempx,tempy;
- int r1,r2;
- oldz = RandFlt(2500.0f,22500.0f);
- // do
- // {
- r1 = rand();
- r2 = rand();
- oldx = ((float) (r1 % (int) sys_glWidth) - sys_glWidth * 0.5f) / (sys_glWidth / oldz);
- oldy = (sys_glHeight * 0.5f - (float) (r2 % (int) sys_glHeight)) / (sys_glWidth / oldz);
- // tempx = (oldx * sys_glWidth / 75.0f) + sys_glWidth/2.0f;
- // tempy = (oldy * sys_glWidth / 75.0f) + sys_glHeight/2.0f;
- // } while (fabs(tempx) < sys_glWidth + 100.0 && fabs(tempy) < sys_glHeight + 100.0);
- deltax = 0.0f;
- deltay = 0.0f;
- deltaz = (float) -starSpeed;
- x = oldx + deltax;
- y = oldy + deltay;
- z = oldz + deltaz;
- r = RandFlt(0.125f,1.0f);
- g = RandFlt(0.125f,1.0f);
- b = RandFlt(0.125f,1.0f);
- animFrame = 0;
- }
-